/* eslint-disable no-unused-vars */
const number = (rule, value, callback) => {
  const reg = /^\d+$|^\d+[.]?\d+$/
  if (!value) {
    return callback(new Error('不能为空'))
  }
  if (value < 0) {
    return callback(new Error('不能小于0'))
  }
  if (!reg.test(value)) {
    callback(new Error('请输入数字值'))
  } else {
    callback()
  }
}
const numberTwoDecima = (rule, value, callback) => {
  const reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/
  if (!value) {
    callback()
  }
  if (value < 0) {
    return callback(new Error('不能小于0'))
  }
  if (!reg.test(value)) {
    callback(new Error('请输入正确的数字值，最多两个小数'))
  } else {
    callback()
  }
}
const telephone = (rule, value, callback) => {
  const reg = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
  if (!reg.test(value)) {
    callback(new Error('请输入正确的固话'))
  } else {
    callback()
  }
}
const postiveInteger = (rule, value, callback) => {
  const reg = /^[0-9]{0,10}$/
  if (!reg.test(value)) {
    callback(new Error('请输入正整数'))
  } else if (value > 999) {
    callback(new Error('最大值为999'))
  } else if (value <= 0) {
    callback(new Error('必须大于0'))
  } else {
    callback()
  }
}
const postiveIntegers = (rule, value, callback) => {
  const reg = /^[0-9]{0,10}$/
  if (!reg.test(value)) {
    callback(new Error('请输入正整数'))
  } else if (!value) {
    return callback(new Error('不能为空'))
  } else if (value <= 0) {
    callback(new Error('必须大于0'))
  } else {
    callback()
  }
}
const uEditor = (rule, value, callback) => {
  if (!value || value === '<p><br></p>') {
    return callback(new Error('请输入内容'))
  }
  callback()
}
const zeorTo100 = (rule, value, callback) => {
  const reg = /^\d+$|^\d+[.]?\d+$/
  if (!value) {
    return callback(new Error('不能为空'))
  }
  if (!reg.test(value)) {
    return callback(new Error('请输入数字值'))
  }
  if (value < 0) {
    return callback(new Error('不能小于0'))
  }
  if (value > 1000) {
    callback(new Error('最大值为1000'))
  } else {
    callback()
  }
}
const Maxnumber = (rule, value, callback) => {
  const reg = /^\d+$|^\d+[.]?\d+$/
  if (!value) {
    return callback(new Error('不能为空'))
  }
  if (!reg.test(value)) {
    return callback(new Error('请输入数字值'))
  }
  if (value < 0) {
    return callback(new Error('不能小于0'))
  }
  if (value > 99.9) {
    callback(new Error('最大值为99.9'))
  } else {
    callback()
  }
}
const Minnumber = (rule, value, callback) => {
  // eslint-disable-next-line no-useless-escape
  const reg = /^(\-|\+)?\d+(\.\d+)?$/
  if (!value) {
    return callback(new Error('不能为空'))
  }
  if (!reg.test(value)) {
    return callback(new Error('请输入数字值'))
  }
  if (value < -99.9) {
    return callback(new Error('不能小于-99.9'))
  }
  if (value > 99.9) {
    callback(new Error('最大值为99.9'))
  } else {
    callback()
  }
}

const validatePassword = (rule, value, callback) => {
  const passwordreg = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/
  if (!passwordreg.test(value)) {
    callback(new Error('字母、数字和符号至少包含两种(不含空格)，长度：6-20'))
  } else {
    callback()
  }
}

const validateMap = {
  name: { max: 50, message: '输入长度不能超过50个字符' },
  input: { max: 50, message: '输入长度不能超过50个字符' },
  input20: { max: 20, message: '输入长度不能超过20个字符' },
  remark: { max: 255, message: '输入长度不能超过255个字符' },
  inputNumber: { type: 'number', message: '只能输入数字', max: 999999999 },
  select: { type: 'number' },
  radio: { type: 'number' },
  require: { required: true, message: '此项必填', trigger: 'blur' },
  requireChange: { required: true, message: '此项必填', trigger: 'change' },
  code: {
    // eslint-disable-next-line
    pattern: /^[a-z_A-Z0-9-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/\",'<>~\·`\?:;|]{0,20}$/,
    message: '编号只能由英文字母、数字、符号组成，并且不超过20个字符'
  },
  telephone: {
    pattern: /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/,
    message: '请输入正确合法的固话'
  },
  phone: {
    // eslint-disable-next-line
    pattern: /^1(3|4|5|6|7|8|9)\d{9}$/,
    message: '请输入正确的手机号'
  },
  email: {
    type: 'email',
    message: '请输入正确的邮箱'
  },
  sort: {
    type: 'number',
    min: 0,
    max: 1000,
    message: '序号范围为0 - 1000'
  },
  number: {
    validator: number, trigger: 'blur'
  },
  maxNumber: {
    validator: Maxnumber, trigger: 'blur'
  },
  minNumber: {
    validator: Minnumber, trigger: 'blur'
  },
  numberTwoDecima: {
    validator: numberTwoDecima, trigger: 'blur'
  },
  zeorTo100: {
    validator: zeorTo100, trigger: 'change'
  },
  uEditor: {
    validator: uEditor
  },
  password: {
    validator: validatePassword, trigger: 'blur'
  },
  postiveInteger: {
    validator: postiveInteger, trigger: 'blur'
  },
  postiveIntegers: {
    validator: postiveIntegers, trigger: 'blur'
  }
}

function genSort (options = {}) {
  const { label = '排序', span = 12 } = options
  return {
    label,
    span,
    width: 80,
    prop: 'sort',
    addDisplay: true,
    editDisplay: true,
    viewDisplay: true,
    type: 'number',
    rules: [validateMap.sort, validateMap.require],
    value: 0,
    minRows: 0,
    maxRows: 1000,
    sortable: true
  }
}

function genUpdateTime (label = '排序') {
  return {
    label: '创建时间',
    width: 140,
    prop: 'updateTime',
    addDisplay: false,
    editDisplay: false,
    viewDisplay: true,
    sortable: true,
  }
}

export {
  validateMap,
  genSort,
  genUpdateTime,
  number
}
